<?php

namespace app\bis\controller;

use think\Controller;

// 数据统计
class Stats extends Base {

    // 收款订单列表
    public function orderList() {
        $merchant_id = session('bisAccount.merchant_id', '', 'bis');//商户号

        /* 时间段 */
        $datetype = input('datetype');
        switch ($datetype) {
            case 'wk'://最近一周数据
                $starttime = date('Y-m-d', strtotime('-6days'));
                $endtime = date('Y-m-d');
                $stime = strtotime($starttime);
                $etime = strtotime(date('Y-m-d', strtotime('+1days')));
                break;
//            case 'tdy': //今日数据
//                $starttime = date('Y-m-d');
//                $endtime = date('Y-m-d');
//                $stime = strtotime($starttime);
//                $etime = strtotime("+1days", strtotime($endtime));
//                break;
            case 'ydy': //昨日数据 
                $starttime = date('Y-m-d', strtotime("-1days"));
                $endtime = date('Y-m-d', strtotime("-1days"));
                $stime = strtotime($starttime);
                $etime = strtotime("+1days", strtotime($endtime));
                break;
            default: //默认当天数据或有时间段查询操作
                $stime = input('stime', 0);
                $etime = input('etime', 0);
//                $shour = input('shour', 0);
//                $ehour = input('ehour', 0);
                if (($stime == 0 && $etime == 0) || ($stime == $etime && $stime == date('Y-m-d'))) {
                    $datetype = "tdy";
                } elseif ($stime == $etime && $stime == date('Y-m-d', strtotime("-1days"))) {
                    $datetype = "ydy";
                } elseif ($stime == date('Y-m-d', strtotime("-6days")) && $etime == date('Y-m-d')) {
                    $datetype = "wk";
                }
                $starttime = $stime != 0 ? $stime : date('Y-m-d');
                $endtime = $etime != 0 ? $etime : date('Y-m-d');
                if ($stime != 0) {
                    $stime = strtotime($stime); //$stime.' '.$shour
                    $etime = strtotime("+1days", strtotime($etime));
                    ; //$etime.' '.$ehour
                } else {
                    $stime = strtotime(date('Y-m-d'));
                    $etime = strtotime(date('Y-m-d', strtotime('+1days')));
                }
                break;
        }
        
        $map = array(); //查询条件
        $map['create_time'] = ['between', [$stime, $etime]];
        
        $status = input('status', 0); //默认显示全部状态数据
        if ($status != 0) {
            $map['status'] = ['=', $status];
        }
        
        //符合条件的订单
        $orderlist = db('pay_bis_order')->where('merchant_id',$merchant_id)->where($map)->select();

        //订单统计
        $totalAmount = 0;
        $totalFee = 0;
        $totalActualAmount = 0;
        $totalNum = 0;
        foreach ($orderlist as &$value) {
            $value['pay_amount'] /= 100;
            $value['fee'] = ($value['pay_fee']+$value['mchnt_fee'])/100;
            $value['actual_amount'] /= 100;
            $totalAmount += $value['pay_amount'];
            $totalFee += $value['fee'];
            $totalActualAmount += $value['actual_amount'];
            $totalNum += 1;
        }
        
        $this->assign('status', $status);
        $this->assign('datetype', $datetype);
        $this->assign('starttime', $starttime);
        $this->assign('endtime', $endtime);
        $this->assign('totalAmount', $totalAmount);
        $this->assign('totalFee', $totalFee);
        $this->assign('totalActualAmount', $totalActualAmount);
        $this->assign('totalNum', $totalNum);
        $this->assign('orderlist', $orderlist);
        return $this->fetch();
    }

    //订单详情
    public function orderDetail() {
        $oid = input('pay_orderid');
        if (!!$oid) {
            $order = db('pay_bis_order')->where('pay_orderid', $oid)->find();
            if($order['merchant_id'] != session('bisAccount.merchant_id', '', 'bis')){
                return '订单号有误，请重试';
            }
            $order['pay_amount'] /= 100;
            $order['fee'] = ($order['pay_fee']+$order['mchnt_fee'])/100;
            $order['actual_amount'] /= 100;
            $this->assign('order', $order);
            return $this->fetch();
        } else {
            return '无订单号，请重试';
        }
    }

    //代付代发记录
    public function withdrawal() {
        $merchant_id = session('bisAccount.merchant_id', '', 'bis');//商户号

        /* 时间段 */
        $datetype = input('datetype');
        switch ($datetype) {
            case 'wk'://最近一周数据
                $starttime = date('Y-m-d', strtotime('-6days'));
                $endtime = date('Y-m-d');
                $stime = strtotime($starttime);
                $etime = strtotime(date('Y-m-d', strtotime('+1days')));
                break;
//            case 'tdy': //今日数据
//                $starttime = date('Y-m-d');
//                $endtime = date('Y-m-d');
//                $stime = strtotime($starttime);
//                $etime = strtotime("+1days", strtotime($endtime));
//                break;
            case 'ydy': //昨日数据 
                $starttime = date('Y-m-d', strtotime("-1days"));
                $endtime = date('Y-m-d', strtotime("-1days"));
                $stime = strtotime($starttime);
                $etime = strtotime("+1days", strtotime($endtime));
                break;
            default: //默认当天数据或有时间段查询操作
                $stime = input('stime', 0);
                $etime = input('etime', 0);
//                $shour = input('shour', 0);
//                $ehour = input('ehour', 0);
                if (($stime == 0 && $etime == 0) || ($stime == $etime && $stime == date('Y-m-d'))) {
                    $datetype = "tdy";
                } elseif ($stime == $etime && $stime == date('Y-m-d', strtotime("-1days"))) {
                    $datetype = "ydy";
                } elseif ($stime == date('Y-m-d', strtotime("-6days")) && $etime == date('Y-m-d')) {
                    $datetype = "wk";
                }
                $starttime = $stime != 0 ? $stime : date('Y-m-d');
                $endtime = $etime != 0 ? $etime : date('Y-m-d');
                if ($stime != 0) {
                    $stime = strtotime($stime); //$stime.' '.$shour
                    $etime = strtotime("+1days", strtotime($etime));
                    ; //$etime.' '.$ehour
                } else {
                    $stime = strtotime(date('Y-m-d'));
                    $etime = strtotime(date('Y-m-d', strtotime('+1days')));
                }
                break;
        }
        
        $map = array(); //查询条件
        $map['create_time'] = ['between', [$stime, $etime]];
        
        $status = input('status', 999); //默认显示全部数据
        if ($status != 999) {
            $map['status'] = ['=', $status];
        }
        $bank_status = input('bank_status', 'all'); //默认显示全部数据
        if ($bank_status != 'all') {
            $map['bank_status'] = ['=', $bank_status];
        }
        
        //符合条件的订单
        $withdrawal = db('pay_daifu_apply')->where('merchant_id',$merchant_id)->where($map)->order('create_time', 'desc')->select();

        //订单统计
        $totalAmount = 0;
        $totalFee = 0;
        $totalNum = 0;
        foreach ($withdrawal as &$value) {
            $value['amount'] /= 100;
            $value['fee'] /= 100;
            $totalAmount += $value['amount'];
            $totalFee += $value['fee'];
            $totalNum += 1;
        }
        
        $this->assign('status', $status);
        $this->assign('bank_status', $bank_status);
        $this->assign('datetype', $datetype);
        $this->assign('starttime', $starttime);
        $this->assign('endtime', $endtime);
        $this->assign('totalAmount', $totalAmount);
        $this->assign('totalFee', $totalFee);
        $this->assign('totalNum', $totalNum);
        $this->assign('withdrawal', $withdrawal);
        return $this->fetch();
    }

    //代付代发详情
    public function withdrawalDetail() {
        $oid = input('mem_order', 0);
        $draw = db('pay_daifu_apply')->where('mem_order', $oid)->find();
        if($draw['merchant_id'] != session('bisAccount.merchant_id', '', 'bis')){
            return '订单号有误，请重试';
        }
        $this->assign('draw', $draw);
        return $this->fetch();
    }

}
